using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.Win32;
using OpcUaHelper.Forms;
using WIT.DAQ.Core.Helper;

namespace WIT.DAQ.Core.Forms;

/// <summary>
/// 命令窗口
/// </summary>
internal class CommandForm : Form
{
	/// <summary>
	/// 解除生命周期限制
	/// </summary>
	private const string COMMAND_STR_SURVIVE = "SURVIVE";

	/// <summary>
	/// 设置deadline
	/// </summary>
	private const string COMMAND_STR_DEADLINE = "SET DEADLINE @";

	private const string COMMAND_STR_COUNTDOWN = "SET COUNTDOWN @";

	private const string COMMAND_STR_BLOCKPLUGIN = "BLOCK PLUGIN";

	private const string COMMAND_STR_SICK = "SICK @";

	private const string COMMAND_STR_PLUGIN = "PLUGIN @";

	/// <summary>
	/// 打开OPCUAHelper
	/// </summary>
	private const string COMMAND_STR_OPCUAHelper = "OPEN OPCUAHELPER";

	/// <summary>
	/// 打开OPCUAHelper （使用默认服务器）
	/// </summary>
	private const string COMMAND_STR_OPCUAHelperD = "OPEN OPCUAHELPER -D";

	/// <summary>
	/// Required designer variable.
	/// </summary>
	private IContainer components = null;

	private TextBox txtCommand;

	private Label label1;

	private Button btnExcute;

	private Button btnCancel;

	/// <summary>
	///
	/// </summary>
	internal CommandForm()
	{
		InitializeComponent();
	}

	private void CommandForm_Load(object sender, EventArgs e)
	{
		txtCommand.Select();
	}

	/// <summary>
	/// 执行命令
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void btnExcute_Click(object sender, EventArgs e)
	{
		string text = txtCommand.Text.Trim().ToUpper();
		if (string.IsNullOrEmpty(text))
		{
			return;
		}
		switch (text)
		{
		case "OPEN OPCUAHELPER":
		{
			FormBrowseServer formBrowseServer = new FormBrowseServer();
			formBrowseServer.Show();
			break;
		}
		case "OPEN OPCUAHELPER -D":
		{
			FormBrowseServer formBrowseServer2 = new FormBrowseServer("opc.tcp://118.24.36.220:62547/DataAccessServer");
			formBrowseServer2.Show();
			break;
		}
		case "SURVIVE":
		{
			if (!RegistryHelper.DeleteValue(Registry.LocalMachine, "SOFTWARE\\WIT.DAQ", "deadline", out var errMsg5))
			{
				MethodBase currentMethod8 = MethodBase.GetCurrentMethod();
				MessageBox.Show(currentMethod8.DeclaringType.FullName + "发生异常：" + errMsg5 + "\r可能需要管理员权限");
				Environment.Exit(0);
			}
			if (File.Exists(Common.DEADLINE_FILEPATH))
			{
				File.Delete(Common.DEADLINE_FILEPATH);
			}
			if (!RegistryHelper.DeleteValue(Registry.LocalMachine, "SOFTWARE\\WIT.DAQ", "countdown", out var errMsg6))
			{
				MethodBase currentMethod9 = MethodBase.GetCurrentMethod();
				MessageBox.Show(currentMethod9.DeclaringType.FullName + "发生异常：" + errMsg6 + "\r可能需要管理员权限");
				Environment.Exit(0);
			}
			if (File.Exists(Common.COUNTDOWN_FILEPATH))
			{
				File.Delete(Common.COUNTDOWN_FILEPATH);
			}
			if (!RegistryHelper.DeleteValue(Registry.LocalMachine, "SOFTWARE\\WIT.DAQ", "scd", out var errMsg7))
			{
				MethodBase currentMethod10 = MethodBase.GetCurrentMethod();
				MessageBox.Show(currentMethod10.DeclaringType.FullName + "发生异常：" + errMsg7 + "\r可能需要管理员权限");
				Environment.Exit(0);
			}
			break;
		}
		default:
			if (text.StartsWith("SET DEADLINE @"))
			{
				if (DateTime.TryParse(text.Substring(text.IndexOf("@") + 1), out var result))
				{
					long num = TimeHelper.ConvertToTimestamp(result);
					RegistryKey registryKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\WIT.DAQ");
					try
					{
						registryKey.SetValue("deadline", num, RegistryValueKind.String);
					}
					catch (Exception ex)
					{
						MethodBase currentMethod = MethodBase.GetCurrentMethod();
						MessageBox.Show(currentMethod.DeclaringType.FullName + "发生异常：" + ex.Message + "\r可能需要管理员权限");
						Environment.Exit(0);
					}
					finally
					{
						registryKey.Close();
					}
					StreamWriter streamWriter = new StreamWriter(Common.DEADLINE_FILEPATH, append: false);
					streamWriter.WriteAsync(num.ToString());
					streamWriter.Flush();
					streamWriter.Close();
				}
				else
				{
					MessageBox.Show("时间格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				}
				break;
			}
			if (text.StartsWith("SET COUNTDOWN @"))
			{
				if (int.TryParse(text.Substring(text.IndexOf("@") + 1), out var result2))
				{
					if (!RegistryHelper.WriteValue(Registry.LocalMachine, "SOFTWARE\\WIT.DAQ", "countdown", result2, out var errMsg))
					{
						MethodBase currentMethod2 = MethodBase.GetCurrentMethod();
						MessageBox.Show(currentMethod2.DeclaringType.FullName + "发生异常：" + errMsg + "\r可能需要管理员权限");
						Environment.Exit(0);
					}
					StreamWriter streamWriter2 = new StreamWriter(Common.COUNTDOWN_FILEPATH, append: false);
					streamWriter2.WriteAsync(result2.ToString());
					streamWriter2.Flush();
					streamWriter2.Close();
				}
				else
				{
					MessageBox.Show("Hours格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				}
				break;
			}
			if (text.StartsWith("SICK @"))
			{
				if (int.TryParse(text.Substring(text.IndexOf("@") + 1), out var result3))
				{
					if (!RegistryHelper.WriteValue(Registry.LocalMachine, "SOFTWARE\\WIT.DAQ", "scd", result3, out var errMsg2))
					{
						MethodBase currentMethod3 = MethodBase.GetCurrentMethod();
						MessageBox.Show(currentMethod3.DeclaringType.FullName + "发生异常：" + errMsg2 + "\r可能需要管理员权限");
						Environment.Exit(0);
					}
				}
				else
				{
					MessageBox.Show("Hours格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				}
				break;
			}
			if (text.StartsWith("BLOCK PLUGIN"))
			{
				if (!RegistryHelper.DeleteValue(Registry.LocalMachine, "SOFTWARE\\WIT.DAQ", "plugincd", out var errMsg3))
				{
					MethodBase currentMethod4 = MethodBase.GetCurrentMethod();
					MessageBox.Show(currentMethod4.DeclaringType.FullName + "发生异常：" + errMsg3 + "\r可能需要管理员权限");
					Environment.Exit(0);
				}
				if (!RegistryHelper.UpdateValue(Registry.LocalMachine, "SOFTWARE\\WIT.DAQ", "plugin", -1, out errMsg3))
				{
					MethodBase currentMethod5 = MethodBase.GetCurrentMethod();
					MessageBox.Show(currentMethod5.DeclaringType.FullName + "发生异常：" + errMsg3 + "\r可能需要管理员权限");
					Environment.Exit(0);
				}
				break;
			}
			if (text.StartsWith("PLUGIN @"))
			{
				if (int.TryParse(text.Substring(text.IndexOf("@") + 1), out var result4))
				{
					int num2 = ((result4 <= 0) ? 1 : 0);
					if (!RegistryHelper.WriteValue(Registry.LocalMachine, "SOFTWARE\\WIT.DAQ", "plugin", num2, out var errMsg4))
					{
						MethodBase currentMethod6 = MethodBase.GetCurrentMethod();
						MessageBox.Show(currentMethod6.DeclaringType.FullName + "发生异常：" + errMsg4 + "\r可能需要管理员权限");
						Environment.Exit(0);
					}
					if (!RegistryHelper.WriteValue(Registry.LocalMachine, "SOFTWARE\\WIT.DAQ", "plugincd", result4, out errMsg4))
					{
						MethodBase currentMethod7 = MethodBase.GetCurrentMethod();
						MessageBox.Show(currentMethod7.DeclaringType.FullName + "发生异常：" + errMsg4 + "\r可能需要管理员权限");
						Environment.Exit(0);
					}
				}
				else
				{
					MessageBox.Show("Hours格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				}
				break;
			}
			MessageBox.Show("未知命令", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			return;
		}
		Close();
	}

	/// <summary>
	/// 回车键执行命令
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void txtCommand_KeyDown(object sender, KeyEventArgs e)
	{
		if (e.KeyCode == Keys.Return)
		{
			btnExcute_Click(null, null);
		}
	}

	private void btnCancel_Click(object sender, EventArgs e)
	{
		Close();
	}

	/// <summary>
	/// ESC关闭
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void CommandForm_KeyDown(object sender, KeyEventArgs e)
	{
		if (e.KeyCode == Keys.Escape)
		{
			btnCancel_Click(null, null);
		}
	}

	/// <summary>
	/// Clean up any resources being used.
	/// </summary>
	/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	/// <summary>
	/// Required method for Designer support - do not modify
	/// the contents of this method with the code editor.
	/// </summary>
	private void InitializeComponent()
	{
		this.txtCommand = new System.Windows.Forms.TextBox();
		this.label1 = new System.Windows.Forms.Label();
		this.btnExcute = new System.Windows.Forms.Button();
		this.btnCancel = new System.Windows.Forms.Button();
		base.SuspendLayout();
		this.txtCommand.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.txtCommand.Location = new System.Drawing.Point(59, 18);
		this.txtCommand.MaxLength = 0;
		this.txtCommand.Name = "txtCommand";
		this.txtCommand.Size = new System.Drawing.Size(313, 21);
		this.txtCommand.TabIndex = 7;
		this.txtCommand.KeyDown += new System.Windows.Forms.KeyEventHandler(txtCommand_KeyDown);
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(12, 21);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(41, 12);
		this.label1.TabIndex = 14;
		this.label1.Text = "命令：";
		this.btnExcute.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnExcute.Location = new System.Drawing.Point(216, 56);
		this.btnExcute.Name = "btnExcute";
		this.btnExcute.Size = new System.Drawing.Size(75, 23);
		this.btnExcute.TabIndex = 17;
		this.btnExcute.Text = "执行(&E)";
		this.btnExcute.UseVisualStyleBackColor = true;
		this.btnExcute.Click += new System.EventHandler(btnExcute_Click);
		this.btnCancel.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnCancel.Location = new System.Drawing.Point(297, 56);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 23);
		this.btnCancel.TabIndex = 18;
		this.btnCancel.Text = "取消(&C)";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(384, 91);
		base.Controls.Add(this.txtCommand);
		base.Controls.Add(this.label1);
		base.Controls.Add(this.btnExcute);
		base.Controls.Add(this.btnCancel);
		base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
		base.KeyPreview = true;
		base.MaximizeBox = false;
		base.MinimizeBox = false;
		base.Name = "CommandForm";
		this.Text = "命令窗口";
		base.Load += new System.EventHandler(CommandForm_Load);
		base.KeyDown += new System.Windows.Forms.KeyEventHandler(CommandForm_KeyDown);
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
